<?php
/*
 combat parser, for ogame.com.cn only
*/

//include_once "languages.php";
include_once "attributes.php";
include_once("../debug.php");

class combatParser {

	var $error = "";     // Fehlermeldung fals eine Methode false zur?fert

	var $dbusername  = "";
	var $dbpassword  = "";
	var $dbname      = "";
	var $dbhost      = "";
	var $tablename   = ""; // combat report Tabelle

	/**
	* @return downloads
	* @param String $dbusername Datenbanknutzername
	* @param String $dbpassword Datenbankpasswort
	* @param String $dbname Name der Datenbank
	* @param String $tablename Name der Tabelle mit den Downloads
	* @param String $dbhost Datenbankadresse
	* @desc Konstruktor der Klasse downloads
 	*/
	function combatparser($dbusername,$dbpassword,$dbname,$tablename,$dbhost="localhost") {

		if (!($this->_set_dbusername($dbusername))) die($this->error);
		if (!($this->_set_dbname($dbname))) die($this->error);
		if (!($this->_set_tablename($tablename))) die($this->error);
		$this->dbpassword = $dbpassword;
		$this->_set_dbhost($dbhost);
		$this->_db_connection_works(); // Testen der Datenbankverbindung
	}


	function parseString($reportstring,$return=false) {
		global $default_server;
		$userid = $_SESSION['s_userid'];
		$ogameserver = $_SESSION['s_ogameserver'];

		$returnarray = array(); // wird ben?t bei return = true;
		
		$reportstring = trim($reportstring);
		
		$string = $reportstring;

		if($ogameserver == 'ogame.com.cn') {
			/* 下列的舰队互相对立 2008-03-24 13:38:33 , 准备进入战斗：: 
			 * + v0.78c, 2008/03/25 : 时间从mm-dd => yyyy-mm-dd
			 */
			$_ntitle = "下列的舰队互相对立";
			$pattern_title = "/^". $_ntitle . "[^\d]+(\d+-)?(\d+-\d+ \d+:\d+:\d+) /";
			$_nattacker = "进攻者";
			$_ndefender = "防御者";
			$_nwin = "赢得了这次战役!";
			$_nunit = "单位";
			$_nlost = "一共损失了";
			$_nplunder = "他掠取了";
			$_nmetal = "金属";
			$_ncrystal = "晶体";
			$_ndeuterium = "重氢";
			$_nand = "和";
			$_ndebris = "在这个空间坐标上有";
		} else if($ogameserver == 'ogame.org') {
			/* On 08-20 15:41:29 the following fleets met in battle:: */
			$_ntitle = "the following fleets met in battle::";
			$pattern_title = "/^On[^\d]+(\d+-)?(\d+-\d+ \d+:\d+:\d+) ". $_ntitle . "/";
			$_nattacker = "Attacker";
			$_ndefender = "Defender";
			$_nwin = " has won the battle!";
			$_nunit = "units";
			$_nlost = "lost a total of";
			$_nplunder = "He captured";
			$_nmetal = "metal";
			$_ncrystal = "crystal";
			$_ndeuterium = "deuterium";
			$_nand = "and";
			$_ndebris = "At these space coordinates now float";			
		} else if($ogameserver == 'ogame.tw') {
			/* 
				下列的艦隊互相對立 08-23 12:20:27 , 準備進入戰鬥::
			*/
			$_ntitle = "下列的艦隊互相對立";
			$pattern_title = "/^". $_ntitle . "[^\d]+(\d+-)?(\d+-\d+ \d+:\d+:\d+) /";
			$_nattacker = "進攻者";
			$_ndefender = "防禦者";
			$_nwin = "贏得了這次戰役!";
			$_nunit = "單位";
			$_nlost = "一共損失了";
			$_nplunder = "他掠取了";
			$_nmetal = "金屬";
			$_ncrystal = "晶體";
			$_ndeuterium = "重氫";
			$_nand = "和";
			$_ndebris = "在這個空間坐標上有";			
		}

		if(strpos($string, $_ntitle, strlen($_ntitle)) !== false) {
			$this->error = "Only one combat report allowed.";
			return false;
		}
		$matches = array();
		if(preg_match($pattern_title, $string, $matches) == 1) {
			if($matches[1]) {
				$combattime = $matches[1] . $matches[2];
			} else {
				$combattime = date("Y") . "-" . $matches[2];
			}
		} else {
			$this->error = "not a valid combat report, parse failed.";
			return false;
		}

		$pattern = "/\n". $_nattacker . " ([^(:]+) \(\[?(\d+:\d+:\d+)\]?\)\n/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$attacker = $matches[1];
			$acoordinates = $matches[2];
		} else {
			$this->error = "not a valid combat report, parse failed.";
			return false;
		}

		$pattern = "/\n". $_ndefender . " ([^(:]+) \(\[?(\d+:\d+:\d+)\]?\)\n/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$defender = $matches[1];
			$dcoordinates = $matches[2];
		} else {
			$this->error = "not a valid combat report, parse failed.";
			return false;
		}

		// DB Verbindung erstellen
		mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die(PARSER_ERROR3);
		mysql_select_db($this->dbname) or die(PARSER_ERROR2);

		$query = "SELECT id from $this->tablename where userid=$userid and acoordinates='$acoordinates' and dcoordinates='$dcoordinates' and combattime='$combattime'";
		$res = mysql_query($query);
		if (!$res) {
			$this->error = $query." ".mysql_error();
			return false;
		}
		if(mysql_fetch_row($res)){
			$this->error = "This battle report has already been uploaded.";
			return false;
		}
		
		if(strpos($string, strtolower($_nattacker) . $_nwin) !== false) {
			$winner = 'attacker';
		} else if(strpos($string, strtolower($_ndefender) . $_nwin) !== false) {
			$winner = 'defender';
		} else {
			$winner = 'deuce';
		}
		
		$p_metal = 0; $p_crystal = 0; $p_deuterium = 0;
		$pattern = "/\n". $_nplunder . "\n([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)[^\d\n]+\n/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$p_metal = str_replace(".", "", $matches[1]);
			$p_crystal = str_replace(".", "", $matches[2]);
			$p_deuterium = str_replace(".", "", $matches[3]);
		}

		$alost_total = 0;
		$pattern = "/\n". $_nattacker . $_nlost . " ([\d.]+)/";
		if($ogameserver == 'ogame.org') $pattern = "/\nThe ". strtolower($_nattacker) . " " . $_nlost . " ([\d.]+)/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$alost_total = str_replace(".", "", $matches[1]);
		}
		
		$dlost_total = 0;
		$pattern = "/\n". $_ndefender . $_nlost . " ([\d.]+)/";
		if($ogameserver == 'ogame.org') $pattern = "/\nThe ". strtolower($_ndefender) . " " . $_nlost . " ([\d.]+)/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$dlost_total = str_replace(".", "", $matches[1]);
		}

		$debris_metal = 0;
		$debris_crystal = 0;
		$pattern = "/\n". $_ndebris . " ([\d.]+)[^\d]+([\d.]+)[^\d]+/";
		if(preg_match($pattern, $string, $matches) == 1) {
			$debris_metal = str_replace(".", "", $matches[1]);
			$debris_crystal = str_replace(".", "", $matches[2]);
		}

		$userid = $_SESSION['s_userid'];
		$query = "REPLACE INTO " . $this->tablename . " SET userid=$userid, attacker='" . mysql_real_escape_string($attacker) . "', defender='" . mysql_real_escape_string($defender) . "', acoordinates='$acoordinates', " .
			" dcoordinates='$dcoordinates', combattime='$combattime', winner='$winner', p_metal=". $p_metal . ", p_crystal=" . $p_crystal .", " .
			" p_deuterium=" . $p_deuterium . ", alost_total=" . $alost_total . ", dlost_total=" . $dlost_total . ", debris_metal=" . $debris_metal . ", " .
			" debris_crystal=" . $debris_crystal . ", report='" . mysql_real_escape_string($string) . "', date_added='". date("Y-m-d h:i:s") . "'";
		if(!$return) {
			$res = mysql_query($query);
			if (!$res) {
				$this->error = $query." ".mysql_error();
				return false;
			}
		} else {
			$returnarray = array_push($query);
		}
				
		echo "<a style=\"color:#c4c4c4;\" href=\"combatview.php?acoordinates=$acoordinates&dcoordinates=$dcoordinates&combattime=$combattime\">".$attacker."[$acoordinates] attacked ".$defender."[$dcoordinates] at $combattime </a><br/>";

		if ($return) return $returnarray;
		else return true;
	}


	/**
	* @return boolean
	* @param String $tablename Name der Tabelle mit den Downloads
	* @desc Setzt den Tabellennamen f? Datenbankverbindung
    */
	function _set_tablename($tablename) {
		if (!empty($tablename)) {
			$this->tablename = $tablename;
			return true;
		} else {
			$this->error = "Der Tabellenname ist leer!";
			return false;
		}
	}


	/**
	* @return boolean
	* @param String $dbusername Datenbanknutzername
	* @desc Setzt den Username f? Datenbankverbindung
    */
	function _set_dbusername($dbusername) {
		if (!empty($dbusername)) {
			$this->dbusername = $dbusername;
			return true;
		} else {
			$this->error = "Der Username ist leer!";
			return false;
		}
	}

	/**
	* @return boolean
	* @param String $dbpassword Datenbankpasswort
	* @desc Setzt das Passwort f? Datenbankverbindungen
 	*/
	function _set_dbpassword($dbpassword) {
			$this->dbpassword = $dbpassword;
			return true;
	}

	/**
	* @return boolean
	* @param String $dbname Datenbankname
	* @desc Setzt den Namen der zu verwendenden Datenbank
 	*/
	function _set_dbname($dbname) {
		if (!empty($dbname)) {
			$this->dbname = $dbname;
			return true;
		} else {
			$this->error = "Der Datenbankname ist leer!";
			return false;
		}
	}

	/**
	* @return boolean
	* @param String $dbhost Datenbankadresse
	* @desc Setzt die Host-Adresse f? Datenbank
 	*/
	function _set_dbhost($dbhost) {
		$this->dbhost = $dbhost;
		return true;
	}

	/**
	* @return boolean
	* @desc Testet ob die Verbindung zur Datenbank funktioniert
 	*/
	function _db_connection_works() {
		mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die(PARSER_ERROR3);
		mysql_select_db($this->dbname) or die(PARSER_ERROR2);
		mysql_close();
		return true;
	}

}




?>